package com.fqy.ticket.member.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fqy.ticket.common.exception.BusinessException;
import com.fqy.ticket.common.exception.BusinessExceptionEnum;
import com.fqy.ticket.common.util.JwtUtil;
import com.fqy.ticket.common.util.SnowUtil;
import com.fqy.ticket.member.domain.Member;
import com.fqy.ticket.member.domain.MemberExample;
import com.fqy.ticket.member.mapper.MemberMapper;
import com.fqy.ticket.member.req.MemeberLoginReq;
import com.fqy.ticket.member.req.MemeberRegisterReq;
import com.fqy.ticket.member.req.MemeberSendCodeReq;
import com.fqy.ticket.member.resp.MemberLoginResp;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MemberService {
    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);
    @Resource
    private MemberMapper memberMapper;
    public int count() {
        return (int) memberMapper.countByExample(null);
    }
    public long register(MemeberRegisterReq req) {
        String  mobile = req.getMobile();

        Member memberDB = selectByMobile(mobile);
        // 如果成员不为空，说明之前注册过，直接抛出手机号存在异常
        if (ObjectUtil.isNotNull(memberDB)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_EXIST);
        }

        Member member = new Member();
        member.setId(SnowUtil.getSnowflakeNextId());
        member.setMobile(mobile);

        memberMapper.insert(member);
        return member.getId();
    }

    public void sendCode(MemeberSendCodeReq req) {
        String  mobile = req.getMobile();
        Member memberDB = selectByMobile(mobile);

        // 如果手机号不存在，插入记录
        if (ObjectUtil.isNull(memberDB)) {
            LOG.info("手机号不存在，插入记录");
            Member member = new Member();
            member.setId(SnowUtil.getSnowflakeNextId());
            member.setMobile(mobile);
            memberMapper.insert(member);
        }
        else {
            LOG.info("手机号存在，不插入记录");
        }
        //
        // 生成验证码
        // String code = RandomUtil.randomString(4);
        String code = "8888";
        LOG.info("生成短信验证码：{}", code);
    }
    public MemberLoginResp login(MemeberLoginReq req) {
        String  mobile = req.getMobile();
        // 手机号如果不存在，说明之前根本没有获取过验证码，因为获取过验证码，必然已经加到数据库了
        // 此时应提示对方获取验证码
        Member memberDB = selectByMobile(mobile);

        // 如果手机号不存在，抛出异常
        if (ObjectUtil.isNull(memberDB)) {
            throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_NOT_EXIST);
        }
        //
        // 校验验证码
        if ("8888".equals(req.getCode())) {
            MemberLoginResp resp = BeanUtil.copyProperties(memberDB, MemberLoginResp.class);
            Map<String, Object> map = BeanUtil.beanToMap(resp);
            String token = JwtUtil.createToken(resp.getId(), resp.getMobile());
            resp.setToken(token);
            return resp;
        }
        throw new BusinessException(BusinessExceptionEnum.MEMBER_MOBILE_CODE_ERROR);
    }

    private Member selectByMobile(String mobile) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(mobile);
        List<Member> members = memberMapper.selectByExample(example);
        if (CollUtil.isEmpty(members)) {
            return null;
        }
        else {
            return members.get(0);
        }
    }
}
